package com.gushuang.leetcode;

import com.sun.jmx.remote.internal.ArrayQueue;

import java.util.ArrayDeque;
import java.util.Queue;

// https://leetcode.cn/problems/subarray-sum-equals-k
public class subarraySumEqualsK {
    // todo
    public int subarraySum(int[] nums, int k) {
        Queue<Integer> queue = new ArrayDeque<Integer>();
        int sum = 0;
        int count = 0;
        for (int num : nums) {
            while (sum + num >= k && queue.size() >= 0) {
                Integer poll = queue.poll();
                sum = sum - poll;
            }
            if (sum < k) {
                queue.add(num);
                sum = sum + num;
            }
            if (sum == k) {
                count++;
            }
        }
        return count;
    }
}
